package com.gxm.base.datatype.date;

import org.junit.Test;

import java.text.SimpleDateFormat;
import java.time.Clock;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.time.temporal.TemporalAdjusters;
import java.util.Calendar;
import java.util.Date;

/**
 * 
 */
public class DateTimeTest {
    public static void main(String[] args) {
        Calendar cal = Calendar.getInstance();
        System.out.println(cal.get(Calendar.YEAR));
        System.out.println(cal.get(Calendar.MONTH)); // 0 - 11
        System.out.println(cal.get(Calendar.DATE));
        System.out.println(cal.get(Calendar.HOUR_OF_DAY));
        System.out.println(cal.get(Calendar.MINUTE));
        System.out.println(cal.get(Calendar.SECOND));
        // Java 8
        LocalDateTime dt = LocalDateTime.now();
        System.out.println(dt.getYear());
        System.out.println(dt.getMonthValue()); // 1 - 12
        System.out.println(dt.getDayOfMonth());
        System.out.println(dt.getHour());
        System.out.println(dt.getMinute());
        System.out.println(dt.getSecond());
    }
    /**
     *@描述
     *@参数  [i]
     *@返回值  java.lang.String
     *@创建人  郭训民
     *@创建时间  2019/11/9
     *@修改人和其它信息
     */
    public String 注解模版(Integer i){
        System.out.println("方法注解模版测试!");
        return "xx";
    }

    /**
    *@描述 如何取得从 1970 年 1 月 1 日 0 时 0 分 0 秒到现在的毫秒数？
    *@参数
    *@返回值  
    *@创建人  郭训民
    *@创建时间  2019/11/9
    *@修改人和其它信息
    */
    @Test
    public void Test2(){
        Calendar.getInstance().getTimeInMillis(); //第一种方式
        System.currentTimeMillis(); //第二种方式
        // Java 8
        Clock.systemDefaultZone().millis();
        System.out.println(Calendar.getInstance().getTimeInMillis());
        System.out.println(System.currentTimeMillis());
        System.out.println(Clock.systemDefaultZone().millis());
    }
    
    /**
    *@描述 如何取得某月的最后一天？
    *@参数
    *@返回值  
    *@创建人  郭训民
    *@创建时间  2019/11/9
    *@修改人和其它信息
    */
    @Test
    public void Test3(){
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        //获取当前月第一天：
        Calendar c = Calendar.getInstance();
        c.add(Calendar.MONTH, 0);
        c.set(Calendar.DAY_OF_MONTH,1);//设置为 1 号,当前日期既为本月第一天
        String first = format.format(c.getTime());
        System.out.println("===============first:"+first);

        //获取当前月最后一天
        Calendar ca = Calendar.getInstance();
        ca.set(Calendar.DAY_OF_MONTH, ca.getActualMaximum(Calendar.DAY_OF_MONTH));
        String last = format.format(ca.getTime());
        System.out.println("===============last:"+last);

        //Java 8
        LocalDate today = LocalDate.now();
        //本月的第一天
        LocalDate firstday = LocalDate.of(today.getYear(),today.getMonth(),1);
        //本月的最后一天
        LocalDate lastDay =today.with(TemporalAdjusters.lastDayOfMonth());
        System.out.println("本月的第一天"+firstday);
        System.out.println("本月的最后一天"+lastDay);
    }
    /**
    *@描述 如何格式化日期?
    *@参数
    *@返回值  
    *@创建人  郭训民
    *@创建时间  2019/11/9
    *@修改人和其它信息
    */
    @Test
    public void Test4(){
        SimpleDateFormat oldFormatter = new SimpleDateFormat("yyyy/MM/dd");
        Date date1 = new Date();
        System.out.println(oldFormatter.format(date1));

        // Java 8
        DateTimeFormatter newFormatter = DateTimeFormatter.ofPattern("yyyy/MM/dd");
        LocalDate date2 = LocalDate.now();
        System.out.println(date2.format(newFormatter));
    }

    /**
     *@描述 打印昨天的当前时刻？
     *@参数
     *@返回值
     *@创建人  郭训民
     *@创建时间  2019/11/9
     *@修改人和其它信息
     */
    @Test
    public void Test5(){
        Calendar cal = Calendar.getInstance();
        cal.add(Calendar.DATE, -1);
        System.out.println(cal.getTime());
        //java8
        LocalDateTime today = LocalDateTime.now();
        LocalDateTime yesterday = today.minusDays(1);
        System.out.println(yesterday);
    }

    @Test
    public void Test6(){
        LocalDateTime today = LocalDateTime.now();
        LocalDateTime yesterday = today.minusDays(1);
        System.out.println(today.withHour(0)
                .withMinute(0)
                .withSecond(0)
                .withNano(0));
        LocalDateTime beginDateTime = today.withHour(0)
                .withMinute(0)
                .withSecond(0)
                .withNano(0);
        System.out.println(today.isBefore(beginDateTime));
    }

    @Test
    public void test7(){
        // Parses the date
        LocalDateTime dt1
                = LocalDateTime.parse("2021-12-30T23:45:30");
        // Prints the date
        System.out.println(dt1);
        // Parses the date
        LocalDateTime dt2
                = LocalDateTime.parse("2021-12-31T00:00");
        // Prints the date
        System.out.println(dt2);
        // Compares both dates
        System.out.println(dt1.isAfter(dt2));
    }
}
